package com.gitee.huanminabc.jcommon.file;

import com.gitee.huanminabc.jcommon.exception.CommonException;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

/**
 * 简要描述
 *
 * @Author: mac
 * @Date: 2025/4/3 13:52
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class FileReadUtil {
    //字符流 读取文件全部内容
    public static String readAllStr(File file) {
        StringBuilder sb = new StringBuilder();
        try (BufferedReader br =
                     new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath())));) {
            String lin = null;
            while ((lin = br.readLine()) != null) {
                // 每次处理一行
                sb.append(lin).append("\n");
            }
        } catch (Exception e) {
            throw new CommonException(e);
        }
        return sb.toString();

    }

    //字符流 读取文件全部内容,返回List<String>
    public static List<String> readAllStrList(File file) {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath())));) {
            String lin;
            List<String> list = new java.util.ArrayList<>();
            while ((lin = br.readLine()) != null) {
                // 每次处理一行
                list.add(lin);
            }
            return list;
        } catch (Exception e) {
            throw new CommonException(e);
        }
    }


    // 字符流  (每次处理一行)
    public static void readLineStrConsumer(File file, Consumer<String> consumer) {
        try (BufferedReader br =
                     new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath())));) {
            String lin = null;
            Integer lineNum = 0;
            while ((lin = br.readLine()) != null) {
                // 每次处理一行
                consumer.accept(lin);
            }
        } catch (Exception e) {
            throw new CommonException(e);
        }
    }


    /**
     * 读取文件的前多少行
     *
     * @Author: huanmin
     * @Date: 2022/6/19 0:42
     * @param: null
     * @return:
     * @Description: 方法功能和使用详细描述....
     */
    public static void readFrontLineConsumer(File file, int size, Consumer<String> consumer) {
        try (BufferedReader br =
                     new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath())));) {

            String lin = null;
            int count = 0;
            while ((lin = br.readLine()) != null) {
                count++;
                // 每次处理一行
                consumer.accept(lin);
                if (count == size) {
                    return;
                }
            }
        } catch (Exception e) {
            throw new CommonException(e);
        }
    }

    //读取文件第一行
    public static String readFirstLine(File file) {
        AtomicReference<String> str = new AtomicReference<String>();
        readFrontLineConsumer(file, 1, str::set);
        return str.get();
    }

    //读取文件最后一行
    public static String readEndLine(File file) {
        String line = null;
        try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath())));) {
            while (true) {
                String line1 = br.readLine();
                if (line1 == null) {
                    break;
                }
                line = line1;
            }

        } catch (Exception e) {
            throw new CommonException(e);
        }
        return line;
    }

    // 跳跃前多少行开始读取
    public static void readSkipLine(File file, int skip, Consumer<String> consumer) {
        try (BufferedReader br =
                     new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath())));) {
            for (int i = 0; i < skip; i++) {
                br.readLine();
            }
            String lin = null;
            while ((lin = br.readLine()) != null) {
                // 每次处理一行
                consumer.accept(lin);
            }
        } catch (Exception e) {
            throw new CommonException(e);
        }
    }

}
